package com.zjitc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.common.exception.AuthException;
import com.zjitc.serviceedu.entity.*;
import com.zjitc.serviceedu.entity.vo.CourseInfoVo;
import com.zjitc.serviceedu.entity.vo.CoursePublishVo;
import com.zjitc.serviceedu.mapper.EduCourseMapper;
import com.zjitc.serviceedu.service.EduChapterService;
import com.zjitc.serviceedu.service.EduCourseDescriptionService;
import com.zjitc.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjitc.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduCourseMapper baseMapper;

    @Override
    public int saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1向课程表添加课程基本信息
        //将courseInfoVo对象转换为eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int rows = baseMapper.insert(eduCourse);

        if (rows == 0) {
            throw new AuthException("添加失败");
        }
        Long cid = eduCourse.getId();
        // 2向课程表描述表添加数据
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);
        return Math.toIntExact(cid);
    }

    @Override
    @Transactional
    public CourseInfoVo getCourseInfo(Long courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;

    }

    @Override
    @Transactional
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int rows = baseMapper.updateById(eduCourse);
        if (rows == 0) {
            throw new AuthException("更新失败");
        }

        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }

    @Override
    public CoursePublishVo getCoursePublishInfo(Long id) {
        CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(id);
        return coursePublishInfo;
    }

    @Override
    public boolean removeByCouresId(Long id) {
        // 第一步：删除课程
        int deletedCourseCount = baseMapper.deleteById(id);

        if (deletedCourseCount > 0) {
            // 第二步：删除所有相关章节
            boolean deletedChapters = eduChapterService.removeByCourseId(id);

            // 第三步：删除所有相关视频
            boolean deletedVideos = eduVideoService.removeByCourseId(id);

            // 返回章节和视频是否都删除成功
            return deletedChapters && deletedVideos;
        }

        return false;
    }

    @Override
    public List<EduCourse> getJavaList() {
        List<EduCourse> javaList = baseMapper.getJavaList();
        return javaList;
    }

    @Override
    public List<EduCourse> getCssList() {
        List<EduCourse> cssList = baseMapper.getCssList();
        return cssList;
    }

    @Override
    public List<EduCourse> getHotList() {
        List<EduCourse> hotList = baseMapper.getHotList();
        return hotList;
    }

    @Override
    public List<EduTeacher> getTeacherCourse(Long id) {
        List<EduTeacher> teacherCourse = baseMapper.getTeacherCourse(id);
        return teacherCourse;
    }

    @Override
    public boolean updateViewCount(Long courseId) {
        EduCourse course = baseMapper.selectById(courseId); // 获取课程信息
        if (course != null) {
            course.setViewCount(course.getViewCount() + 1); // 浏览量加1
            baseMapper.updateById(course);  // 更新数据库中的数据
            return true;
        }
        return false;
    }

    @Override
    public boolean updateBuyCount(Long courseId) {
        EduCourse course = this.getById(courseId);
        if (course != null) {
            course.setBuyCount(course.getBuyCount() + 1); // 销量+1
            return this.updateById(course); // 更新课程信息
        }
        return false;
    }
}